#pragma once
#include "mtx.hpp"
#include <iostream>
#include <pthread.h>
#include <time.h>
using namespace std;
#define NUM 1000
class ticket
{
public:
    ticket(int size = NUM)
        : size_(size)
    {
        pthread_mutex_init(&mtx_, nullptr);
        pthread_cond_init(&full_, nullptr);
        pthread_cond_init(&empty_, nullptr);
    }
    void buyticket(int sss, pthread_cond_t &ssss)
    {
        mutex s(mtx_);
        while (size_ == 0)
        {
            pthread_cond_signal(&empty_);
            cout << "我是线程" << pthread_self() <<"     " <<sss << "我要睡觉了" << endl;
            pthread_cond_wait(&full_, &mtx_);
            cout << "我是线程" << pthread_self() << "    "<<sss << "我醒来了" << endl;
        }
        cout << pthread_self() <<"     " <<sss << endl;
        size_--;
        pthread_cond_signal(&ssss);
        cout << "我是线程" << pthread_self() <<"    " <<sss << "我叫醒了其他线程" << endl;

    }
    void date()
    {
        mutex s(mtx_);
        while (size_ != 0)
        {
            pthread_cond_signal(&full_);
            cout << "我要睡觉了" << endl;
            pthread_cond_wait(&empty_, &mtx_);
            cout << "我醒来了" << endl;
        }
        size_ = NUM;
    }
    ~ticket()
    {
        pthread_mutex_destroy(&mtx_);
        pthread_cond_destroy(&full_);
        pthread_cond_destroy(&empty_);
    }

public:
    int size_;
    pthread_mutex_t mtx_;
    pthread_cond_t full_;
    pthread_cond_t empty_;
};